//
//  NSMutableDictionary+JSON.m
//  KoalaKnow
//
//  Created by liuny on 15/7/23.
//  Copyright (c) 2015年 szjn. All rights reserved.
//

#import "NSMutableDictionary+JSON.h"

@implementation NSMutableDictionary (JSON)
-(void)setJsonValue:(NSString *)value key:(NSString *)key{
    if(key != nil && key.length > 0){
        self[key] = value==nil?@"":value;
    }
}

-(NSString *)getJsonValue:(NSString *)key{
    NSString *value = self[key];
    if(value == nil || [value isEqual:[NSNull null]]){
        value = @"";
    }else{
        value = [NSString stringWithFormat:@"%@",value];
    }
    return value;
}

-(NSString *)JSONString
{
    if ([NSJSONSerialization isValidJSONObject:self])
    {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
        NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return json;
    }
    return @"";
}
@end
